package rai;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.ObjectInputStream;

import rai.service.rep.ListeRepertoires;
import rai.service.rep.ListeRepertoiresImplement;
import rai.service.transport.RaiSocket;
import rai.service.transport.Serveur;

public class RaiServerListeRepertoires {

	private Serveur serveur;
	private ListeRepertoires lesRepertoires;

	public RaiServerListeRepertoires() throws IOException,
			ClassNotFoundException {
		serveur = new Serveur();
		try {
			ObjectInputStream entree = new ObjectInputStream(
					new FileInputStream("lesRepertoires.obj"));
			lesRepertoires = (ListeRepertoires) entree.readObject();
			entree.close();
		} catch (FileNotFoundException e) {
			lesRepertoires = new ListeRepertoiresImplement();
		}

	}

	public void instancierServeur() throws IOException,
			UtilisateurInconnueException {

		RaiSocket socket;
		while (true) {
			socket = serveur.accept();
			String user = socket.readString();
			try {
				lesRepertoires.authentification(user);
				socket.writeBoolean(true);
				new RaiExecCommandeListeRep(socket, lesRepertoires);
			} catch (UtilisateurInconnueException e1) {
				socket.writeBoolean(false);
				socket.closeSocket();
			}
		}

	}
}
